/*
 * @Author: liuzelin
 * @Date: 2024-04-20 11:07:03
 * @LastEditors: liuzelin
 * @LastEditTime: 2024-04-21 20:42:51
 * @Description: file content
 */
#pragma once
#include "panda_event.hpp"
#include "panda_uv_handler.hpp"
#include "panda_uv_helper.hpp"

#include "uv.h"

#include <condition_variable>
#include <limits>
#include <mutex>

namespace PandaImpl{
    
class STDEvent : public Panda::Event {
 public:
  STDEvent() = default;
  ~STDEvent() = default;
  void Set() override { cv_.notify_one(); }
  void SetAll() { cv_.notify_all(); }
  void Wait( int64_t ms = Event::tForEver ) override {
    std::unique_lock l(mtx_);
    cv_.wait_for(l, std::chrono::milliseconds(ms));
  }

 private:
  std::condition_variable cv_;
  std::mutex mtx_;
};

class ThreadImpl;
class UVHelper;
class UVAsync : public Panda::Event, public UVHandler {
  friend class UVHelper;

 public:
  explicit UVAsync(ThreadImpl *thread);
  void Set() override;
  void Wait(int64_t ms = tForEver) override;
  void OnClose() override {}
  ~UVAsync() = default;

 private:
  ThreadImpl *thread_;
  uv_async_t async_;
};
}